package com.mochimedia.ui {
	
	import com.mochimedia.ui.*;
	import flash.geom.Matrix;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import flash.display.Graphics;
    import flash.display.Sprite;
	import flash.text.TextField;
    
    public class Create {

        public static const ICON_ARROW_RIGHT:Array = [ -0.2, -0.5, 0.4, 0, -0.2, 0.5];
        public static const ICON_ARROW_LEFT:Array = [ 0.2, -0.5, -0.4, 0, 0.2, 0.5];
        public static const ICON_ARROW_UP:Array = [ -0.5, 0.3, 0, -0.3, 0.5, 0.3];
        public static const ICON_ARROW_DOWN:Array = [ -0.5, -0.3, 0, 0.3, 0.5, -0.3];
        public static const ICON_CHECK:Array = [0.5, -0.2, -0.1, 0.4, -0.5, 0, -0.3, -0.2, -0.1, 0, 0.3, -0.4, 0.5, -0.2];
        public static const ICON_PLUS:Array = [0.12, -0.5, 0.12, -0.12, 0.5, -0.12, 0.5, 0.12, 0.12, 0.12, 0.12, 0.5, -0.12, 0.5, -0.12, -0.12, -0.5, -0.12, -0.5, 0.12, -0.12, 0.12, -0.12, -0.5];
        public static const ICON_MINUS:Array = [ -0.5, -0.12, 0.5, -0.12, 0.5, 0.12, -0.5, 0.12];
		
		public static const ICON_PLAY:Array = [ -0.3, -0.5, 0.5, 0, -0.3, 0.5];
		public static const ICON_PAUSE:Array = [-0.1, -0.5, -0.1, 0.5, -0.5, 0.5, -0.5, -0.5, -0.1, -0.5, 0.5, -0.5, 0.5, 0.5, 0.1, 0.5, 0.1, -0.5, 0.5, -0.5];
		public static const ICON_CLOSE:Array = [0.5, -0.5, 0.5, -0.31, 0.18, 0, 0.345, 0.165, 0.35, 0.165, 0.5, 0.315, 0.5, 0.5, 0.31, 0.5, 0, 0.18, -0.095, 0.28, -0.095, 0.28, -0.315, 0.5, -0.5, 0.5, -0.5, 0.315, -0.18, 0, -0.28, -0.095, -0.28, -0.095, -0.5, -0.31, -0.5, -0.5, -0.315, -0.5, -0.145, -0.325, -0.145, -0.325, 0, -0.18, 0.31, -0.5, 0.5, -0.5];
		public static const ICON_NEXTTRACK:Array = [0.5, 0.45, 0.5, -0.46, 0.4, -0.46, 0.4, -0.04, -0.05, -0.46, -0.05, -0.04, -0.5, -0.46, -0.5, 0.45, -0.05, 0.04, -0.05, 0.45, 0.4, 0.04, 0.4, 0.45, 0.5, 0.45];
        public static const ICON_PREVTRACK:Array = [0.5, -0.46, 0.5, 0.45, 0.04, 0.04, 0.04, 0.45, -0.41, 0.04, -0.41, 0.45, -0.5, 0.45, -0.5, -0.46, -0.41, -0.46, -0.41, -0.04, 0.04, -0.46, 0.04, -0.04, 0.5, -0.46];
		public static const ICON_FASTFORWARD:Array = [ -0.5, 0.47, -0.03, 0.04, -0.03, 0.47, 0.5, 0, -0.03, -0.48, -0.03, -0.05, -0.5, -0.48, -0.5, 0.47];
		public static const ICON_REWIND:Array = [0.5, -0.48, 0.5, 0.47, 0.02, 0.04, 0.02, 0.47, -0.5, 0, 0.02, -0.48, 0.02, -0.05, 0.5, -0.48];
		
		//
        //
        public static function button (mc:Sprite, textLabel:String = "", textAlign:Number = -1, width:Number = NaN, height:Number = NaN, tabMode:Boolean = false, groupMode:Boolean = false, toggleMode:Boolean = false, toggledLabel:String = "", style:Style = null):Sprite {
            
            if (style == null) style = new Style();
            
            var cornerRoundness:String = String(style.round);
			
			if (textAlign == -1) textAlign = Position.ALIGN_CENTER;
            if (tabMode) cornerRoundness = cornerRoundness + " " + cornerRoundness + " 0 0";
    
            var button:Sprite = new Sprite();
			button.name = "btn";
            var button_tf:TextField;
            var button_btn:Sprite = new Sprite();
			button_btn.name = "button_btn";
			button_btn.buttonMode = true;
            var button_inactive:Sprite = new Sprite();
			button_inactive.name = "button_inactive";
            var button_tf_inactive:TextField;
            var button_selected:Sprite = new Sprite();
			button_selected.name = "button_selected";
            var button_tf_selected:TextField;
    
            var g:Sprite = mc;
    
            var temp:Array = cornerRoundness.split(" ");
            for (var i:int = 0; i < temp.length; i++) temp[i] = (parseInt(temp[i]) > 0) ? parseInt(temp[i]) - style.borderWidth : 0;
            var innerRoundness:String = temp.join(" ");
    
			mc.addChild(button);
        
            button_tf = newText(textLabel, "_buttontext", button, style, (groupMode) ? style.unselectedTextColor : style.inverseTextColor, style.fontSize);
        
			var twidth:Number = button_tf.width;
			if (toggledLabel != null) {
				button_tf.text = toggledLabel;
				twidth = Math.max(twidth, button_tf.width);
				button_tf.text = textLabel;
			}
			
            if (isNaN(width)) width = twidth + style.padding * 3 + style.borderWidth * 2;
            else width = Math.max(width, twidth + style.padding * 3 + style.borderWidth * 2 - 2);
            
            if (isNaN(height)) height = button_tf.height + style.padding + style.borderWidth * 2;
            else height = Math.max(height, button_tf.height + style.padding + style.borderWidth * 2 - 2);
    
            if (textAlign == Position.ALIGN_LEFT) button_tf.x = style.padding;
            else if (textAlign == Position.ALIGN_RIGHT) button_tf.x = width - button_tf.width - style.padding;   
            else button_tf.x = Math.floor((width - button_tf.width) * 0.5);
            
            button_tf.y = Math.floor((height - button_tf.height) * 0.5);
            
            g = button;
            
            var vpad:Number = 0;
            if (tabMode) vpad = style.borderWidth;
    
            if (style.border) DrawingMethods.roundedRect(g, false, 0, 0, width, height - vpad, cornerRoundness, [style.borderColor], [style.backgroundAlpha], [1]);
            if (style.background) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [(groupMode) ? style.unselectedColor : style.buttonColor], [style.backgroundAlpha], [1]);
            if (style.gradient) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [0xffffff, 0xffffff, 0x000000, 0x000000], [0.40, 0.15, 0, 0.10], [0, 128, 128, 255]);
    
			button.addChild(button_btn);

            g = button_btn;
            DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [0xffffff], [20], [1]);
            button_btn.alpha = 0;
              
			button.addChild(button_selected);

            g = button_selected;
			
			if (!toggleMode) {
				
				if (style.border) DrawingMethods.roundedRect(g, false, 0, 0, width, height - vpad, cornerRoundness, [style.inverseTextColor], [style.backgroundAlpha], [1]);
				if (style.background) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [style.buttonColor], [style.backgroundAlpha], [1]);
				if (style.gradient) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [0xffffff, 0xffffff, 0x000000, 0x000000], [0.4, 0.15, 0, 0.10], [0, 128, 128, 255]);
				button_tf_selected = newText(textLabel, "_buttontext", button_selected, style, style.inverseTextColor, style.fontSize);
				button_tf_selected.x = button_tf.x;
				button_tf_selected.y = button_tf.y;
				button_selected.visible = false;   
				
			} else {
				
				if (style.border) DrawingMethods.roundedRect(g, false, 0, 0, width, height - vpad, cornerRoundness, [style.borderColor], [style.backgroundAlpha], [1]);
				if (style.background) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [ColorTools.getTintedColor(style.buttonColor, 0x000000, 0.2)], [style.backgroundAlpha], [1]);
				if (style.gradient) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [0x000000, 0x000000, 0xffffff, 0xffffff], [0.2, 0, 0.1, 0.2], [0, 128, 128, 255]);
				button_tf_selected = newText((toggledLabel.length > 0) ? toggledLabel : textLabel, "_buttontext", button_selected, style, style.inverseTextColor, style.fontSize);
				if (textAlign == Position.ALIGN_LEFT) button_tf_selected.x = style.padding;
				else if (textAlign == Position.ALIGN_RIGHT) button_tf_selected.x = width - button_tf_selected.width - style.padding;   
				else button_tf_selected.x = Math.floor((width - button_tf_selected.width) * 0.5);
				button_tf_selected.y = button_tf.y;
				button_selected.visible = false;  				
				
				
			}
    
    
            button.addChild(button_inactive);
			
            g = button_inactive;
            if (style.border) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [style.inactiveColor], [style.backgroundAlpha], [1]);
			button_tf_inactive = newText(textLabel, "_buttontext", button_inactive, style, style.inactiveTextColor, style.fontSize);
			button_tf_inactive.x = button_tf.x;
            button_tf_inactive.y = button_tf.y;
            button_inactive.visible = false;
			
			if (toggleMode) button.setChildIndex(button_btn, 3);
            
            return button;
    
        }
        
        //
        //
        public static function hitArea (mc:Sprite, icon:Array, width:Number, height:Number, toggleMode:Boolean = false, toggledIcon:Array = null, style:Style = null):Sprite {
            
            if (style == null) style = new Style();
           
            var button:Sprite = new Sprite();
			button.name = "btn";
            var button_btn:Sprite = new Sprite();
			button_btn.name = "button_btn";
			button_btn.buttonMode = true;
            var button_inactive:Sprite = new Sprite();
			button_inactive.name = "button_inactive";
            var button_selected:Sprite = new Sprite();
			button_selected.name = "button_selected";
		
            var g:Sprite = mc;
    
            mc.addChild(button);
        
            if (style.border) DrawingMethods.roundedRect(g, false, 0, 0, width, height, "0", [style.borderColor], [style.backgroundAlpha], [1]);
            if (style.background) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [style.unselectedColor], [style.backgroundAlpha], [1]);
            else DrawingMethods.rect(g, false, 0, 0, width, height, 0x000000, 0);
			if (style.gradient) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [0xffffff, 0xffffff, 0x000000, 0x000000], [0.4, 0.15, 0, 0.10], [0, 128, 128, 255]);
			if (icon != null) newIcon(icon, g, style.inverseTextColor, 1, style);
            
			button.addChild(button_btn);
            
            g = button_btn;
            DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [0xffffff], [20], [1]);
			button_btn.alpha = 0;
        
            button.addChild(button_selected);
			
            g = button_selected;
			
			if (!toggleMode) {
				
				if (style.border) DrawingMethods.roundedRect(g, false, 0, 0, width, height, "0", [style.borderColor], [style.backgroundAlpha], [1]);
				if (style.background) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [style.buttonColor], [style.backgroundAlpha], [1]);
				else DrawingMethods.rect(g, false, 0, 0, width, height, 0x000000, 0);
				if (style.gradient) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [0xffffff, 0xffffff, 0x000000, 0x000000], [0.4, 0.15, 0, 0.10], [0, 128, 128, 255]);
				if (icon != null) newIcon(icon, g, style.inverseTextColor, 1, style);
				
			} else {
				
				if (style.border) DrawingMethods.roundedRect(g, false, 0, 0, width, height, "0", [style.borderColor], [style.backgroundAlpha], [1]);
				if (style.background) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [style.buttonColor], [style.backgroundAlpha], [1]);
				else DrawingMethods.rect(g, false, 0, 0, width, height, 0x000000, 0);
				if (style.gradient) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [0x000000, 0x000000, 0xffffff, 0xffffff], [0.2, 0, 0.1, 0.2], [0, 128, 128, 255]);
				if (icon != null) newIcon((toggledIcon != null) ? toggledIcon : icon, g, style.inverseTextColor, 1, style);
							
			}
            
            button_selected.visible = false;           
    
            button.addChild(button_inactive);

            g = button_inactive;
			DrawingMethods.rect(g, false, 0, 0, width, height, 0x000000, 0);
            if (style.border) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [style.inactiveColor], [style.backgroundAlpha], [1]);
            if (icon != null) newIcon(icon, g, style.inactiveTextColor, 1, style);
            
            button_inactive.visible = false;

			if (toggleMode) button.setChildIndex(button_btn, 2);
			
            return button;
    
        }
        
        //
        //
        public static function newText (text:String, id:String, container:Sprite, style:Style, color:Number = NaN, size:Number = NaN, html:Boolean = false):TextField {
            
            if (isNaN(color)) color = style.textColor;
            if (isNaN(size)) size = style.fontSize;
    
            var tf:TextField = new TextField();
			tf.width = 200;
			tf.height = 10;
			tf.name = id;
			container.addChild(tf);
			
            tf.autoSize = TextFieldAutoSize.LEFT;
            tf.embedFonts = style.embedFonts;
            
            if (html) {
                tf.htmlText = text;
            } else {
                tf.text = text;
            }

            tf.selectable = false;
            tf.setTextFormat(new TextFormat(style.font, size, color, true));
			tf.defaultTextFormat = new TextFormat(style.font, size, color, true);
			if (style.embedFonts) tf.antiAliasType = AntiAliasType.ADVANCED;
            tf.textColor = color;
    
            return tf;
        
        }
        
        //
        //
        public static function inputText (text:String, id:String, container:Sprite, width:Number, height:Number, style:Style):TextField {
            
            var tf:TextField = newText(text, id, container, style);
            
            tf.selectable = true;
            tf.multiline = false;
            tf.wordWrap = false;
            
            tf.type = "input";
            
            tf.autoSize = TextFieldAutoSize.LEFT;
            
            tf.setTextFormat(new TextFormat("Verdana", style.fontSize, style.textColor, false, false, false, "", "", "left"));
            tf.defaultTextFormat = new TextFormat("Verdana", style.fontSize, style.textColor, false, false, false, "", "", "left");
            
            tf.autoSize = TextFieldAutoSize.NONE;
            
            if (!isNaN(width) && width > 0) {
                tf.width = width;
            }
            
            if (!isNaN(height) && height > 0) {
                tf.height = height;
            } else {
                tf.height = tf.height + 2 + style.borderWidth * 2;
            }
            
            return tf;
            
        }
        
        //
        //
        public static function newIcon (points:Array, mc:Sprite, color:Number, alpha:Number = 1, parentStyle:Style = null):void {
            
            var icon:Sprite = new Sprite();
			icon.name = "icon";
			mc.addChild(icon);
            icon.mouseEnabled = false;
            
            icon.x = mc.width * 0.5;
            icon.y = mc.height * 0.5;
            
            var g:Graphics = icon.graphics;
            
			if (parentStyle == null) parentStyle = new Style();
			
			var dimX:Number = mc.width - parentStyle.borderWidth * 2;
			var dimY:Number = mc.height - parentStyle.borderWidth * 2;
			
            if (points.length > 2) {
                
                g.beginFill(color, alpha);
                g.moveTo(points[0] * dimX * 0.5, points[1] * dimY * 0.5);
                
                for (var i:int = 2; i < points.length; i+=2) {
                    g.lineTo(points[i] * dimX * 0.5, points[i + 1] * dimY * 0.5);
                }
                
                g.endFill();
                
            }
            
        }
		
		//
		//
		public static function background (mc:Sprite, width:Number, height:Number, style:Style = null, border:Boolean = false, round:Number = -1):void {
			
			if (style == null) style = Component.globalStyle;
			
			var b:Number = (border) ? style.borderWidth : 0;
			var r:Number = (round == -1) ? style.round : round;
			
            if (border) DrawingMethods.roundedRect(mc, false, 0, 0, width, height, r.toString(), [style.borderColor], [style.backgroundAlpha], [1]);
            if (style.background && !style.bgGradient) DrawingMethods.roundedRect(mc, false, b, b, width - b * 2, height - b * 2, Math.max(0, r - b).toString(), [style.backgroundColor], [style.backgroundAlpha], [1]);
            else if (style.background && style.bgGradient) {
				var m:Matrix = null;
				if (style.bgGradientHeight > 0) {
					m = new Matrix();
					m.createGradientBox(width - b * 2, style.bgGradientHeight, 90 * (Math.PI / 180), b, b);
				}
				DrawingMethods.roundedRect(mc, false, b, b, width - b * 2, height - b * 2, Math.max(0, r - b).toString(), style.bgGradientColors, [style.backgroundAlpha], style.bgGradientRatios, m);
			}
			
		}
 
    }
	
}
